<template>
  <!-- 放置弹层组件 -->
  <el-dialog
    title="新增部门"
    :visible="hdshowDialog"
    :close-on-click-modal="false"
    @close="$emit('update:hdshowDialog', false)"
  >
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in userList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <template #footer>
      <div>
        <el-button type="primary" size="small" @click="add">确定</el-button>
        <el-button size="small" @click="cancel">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { getDepartmentUser, getAddDepartment } from '@/api/departments'
import { Message } from 'element-ui'
export default {
  // 需要传入一个props变量来控制 显示或者隐藏
  props: {
    hdshowDialog: {
      type: Boolean,
      required: true
    },
    hdData: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      form: {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          // 需要自定义效验 判断部门名称不能与兄弟部门名称相同
          { validator: this.validateName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: this.validateCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '部门简介不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门简介要求1-300个字符', trigger: 'blur' }
        ]
      },
      // 储存负责人的数据
      userList: []
    }
  },
  methods: {
    /**
     * 获取负责人数组函数封装
     */
    async getDepartmentUserList() {
      const { data } = await getDepartmentUser()
      this.userList = data
    },
    // 确定添加按钮兜底效验
    add() {
      this.$refs.form.validate(async(key) => {
        if (!key) return
        // 1.此处pid=''
        // 2.获取数据调用接口
        await getAddDepartment(this.form)
        // 3.提示添加成功
        Message.success('添加成功')
        // 4.关闭弹窗 清除表单数据
        this.cancel()
        // 5.从新获取部门列表数据
        // 触发自定义事件 父组件自己从新调用接口从新渲染页面
        this.$emit('hdadd')
      })
    },
    // 取消按钮
    cancel() {
      this.$refs.form.resetFields()
      this.$emit('update:hdshowDialog', false)
    },
    validateName(rule, value, callback) {
      // 1.首先选出所有一级部门
      const list = this.hdData.filter(e => e.pid === '')
      // 2.判断输入的部门名称是否与已有的一级部门名称一致
      const key = list.some(e => e.name === value)
      key ? callback('部门名称不得重复') : callback()
    },
    validateCode(rule, value, callback) {
      const list = this.hdData
      const key = list.some(e => e.code === value)
      key ? callback('部门编码不得重复') : callback()
    }
  }
}
</script>
